﻿#include "TableViewPropertyEditEntry.h"
#include <QTableView>

#include "BooleanEditEntry.h"
#include "EnumEditEntry.h"

#include "ObjectPropertyEditEntryFactory.h"
REGISTER_PROPERTY_EDIT_ENTRY(TableView)

struct TableViewPropertyEditEntry::TableViewPropertyEditEntryPrivate {
    QPointer<QTableView> view;

    QPointer<BooleanEditEntry> entryShowGrid;
    QPointer<EnumEditEntry> entryGridStyle;
    QPointer<BooleanEditEntry> entrySortingEnabled;
    QPointer<BooleanEditEntry> entryWordWrap;
    QPointer<BooleanEditEntry> entryCornerButtonEnabled;
};

TableViewPropertyEditEntry::~TableViewPropertyEditEntry() { }

TableViewPropertyEditEntry::TableViewPropertyEditEntry(QWidget *parent) : PropertyEditEntry{parent} {
    p.reset(new TableViewPropertyEditEntryPrivate);
    initUi();
    retranslateUi();
}

TableViewPropertyEditEntry::TableViewPropertyEditEntry(void *object, QWidget *parent) : PropertyEditEntry{parent} {
    p.reset(new TableViewPropertyEditEntryPrivate);
    initUi();
    retranslateUi();
    setObject(object);
}

void TableViewPropertyEditEntry::setObject(void *object) {
    p->view = static_cast<QTableView*>(object);
}

void TableViewPropertyEditEntry::onTick() {
    if(!p->view || !isVisible()) {
        return ;
    }

    p->entryShowGrid->setValue(p->view->showGrid());
    p->entryGridStyle->setValue(p->view->gridStyle());
    p->entrySortingEnabled->setValue(p->view->isSortingEnabled());
    p->entryWordWrap->setValue(p->view->wordWrap());
    p->entryCornerButtonEnabled->setValue(p->view->isCornerButtonEnabled());
}

void TableViewPropertyEditEntry::initUi() {
    p->entryShowGrid = new BooleanEditEntry;
    appendChildEntry(p->entryShowGrid);

    p->entryGridStyle = new EnumEditEntry;
    p->entryGridStyle->setMetaEnum(QMetaEnum::fromType<Qt::PenStyle>());
    appendChildEntry(p->entryGridStyle);

    p->entrySortingEnabled = new BooleanEditEntry;
    appendChildEntry(p->entrySortingEnabled);

    p->entryWordWrap = new BooleanEditEntry;
    appendChildEntry(p->entryWordWrap);

    p->entryCornerButtonEnabled = new BooleanEditEntry;
    appendChildEntry(p->entryCornerButtonEnabled);
}

void TableViewPropertyEditEntry::retranslateUi() {
    setTitle(tr("QTableView"));
    p->entryShowGrid->setTitle(tr("showGrid"));
    p->entryGridStyle->setTitle(tr("gridStyle"));
    p->entrySortingEnabled->setTitle(tr("sortingEnabled"));
    p->entryWordWrap->setTitle(tr("wordWrap"));
    p->entryCornerButtonEnabled->setTitle(tr("cornerButtonEnabled"));
}
